/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.handlers;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.neIsInstance;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Interaction;

import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagram.sequence.sedi.edit.CFEditPart;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

// TODO DELETE - NOT USED
@Deprecated
public class DiagramAddCFHandler {

    public final EditPolicy hostPolicy;

    public DiagramAddCFHandler(final EditPolicy hostPolicy) {
        this.hostPolicy = hostPolicy;
    }

    @SuppressWarnings("unchecked")
    public Command[] getAddCommands(CFEditPart cfragmentEP, Rectangle r) {
        // We don't add the child, but rather move it so that it appears
        // to be inside the suggested new parent

        // The constraint is relative to the frame - convert it back
        // to absolute
        // TODO Maybe let the interaction have a pointer to the frame

        GraphElement cfView = (GraphElement) cfragmentEP.getModel();
        Diagram diagram = LookForElement.getDiagram(cfView);

        for (Object diagramElement : ModelUtil.getContents(diagram)) {
            if (neIsInstance(diagramElement, Interaction.class)) {
                final GraphNode interactionView = (GraphNode) diagramElement;
                r.translate(interactionView.getPosition());

                // In addition adjust for the Frame figure insets...
                // TODO Just ask the figure to do all this
                final GraphicalEditPart frameEP =
                        (GraphicalEditPart) cfragmentEP.getViewer().getEditPartRegistry().get(
                                interactionView);
                final IFigure frameFigure = frameEP.getFigure();
                Insets frameInsets = frameFigure.getBorder().getInsets(frameFigure);
                r.translate(frameInsets.left, frameInsets.top);
                break;
            }
        }

        // return createChangeConstraintCommand(cfragmentEP, r);
        return new CombinedFragmentLayoutHandler().getMoveCommands(cfragmentEP, r);
    }

}
